package com.whpost.service.service.t;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.dao.t.TPermissionDao;
import com.whpost.service.dao.t.TXzjgDao;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TXzjg;
import com.whpost.service.entity.t.TPermission;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.tools.ToolBean;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TXzjgServices extends BaseServices {

    @Resource
    private TXzjgDao tXzjgDao;
    @Resource
    private TApiNonceService tApiNonceService;

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String curpage = "",pagesize="",cxmc="";
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("cxmc")) {
                cxmc = jsonObject.getString("cxmc");
            }

            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            Page<TXzjg> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));

            ListInfo<TXzjg> TXzjgListInfo = new ListInfo<>();
            QueryWrapper<TXzjg> tXzjgQueryWrapper = new QueryWrapper<TXzjg>();
            if (!StringUtils.isEmpty(cxmc)){
                tXzjgQueryWrapper.like("name",cxmc);
            }else{
                tXzjgQueryWrapper.eq("1","1");
            }
            tXzjgQueryWrapper.orderByAsc("xh");

            IPage<TXzjg> TXzjgIPage = tXzjgDao.selectPage(page,tXzjgQueryWrapper);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(TXzjgIPage.getTotal());
            pageInfo.setCurPage(TXzjgIPage.getCurrent());
            pageInfo.setPageSize(TXzjgIPage.getSize());
            pageInfo.setPageTotal(TXzjgIPage.getPages());

            TXzjgListInfo.setPageInfo(pageInfo);
            TXzjgListInfo.setListInfo(TXzjgIPage.getRecords());

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",TXzjgListInfo);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo tree(String loginId,Map<String, Object> map) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            Map<String,Object> rtnMap = new HashMap<>();
            TCzy tCzy = gettCzyDao().getCzyById(loginId);
            TXzjg tXzjg = new TXzjg();
            TXzjg tXzjgSon = new TXzjg();
            String treejson="";
            //一级菜单
            List<TXzjg> tXzjgList = new ArrayList<>();
            tXzjgList = tXzjgDao.selectList(new QueryWrapper<TXzjg>().eq("jb","1").orderByAsc("xh"));

            //二级菜单
            List<TXzjg> tXzjgListSon = new ArrayList<>();
            for (int i=0;i<tXzjgList.size();i++){
                tXzjg = new TXzjg();
                tXzjg = tXzjgList.get(i);
                treejson += "{id:\""+tXzjg.getId()+"\",name: \""+tXzjg.getName()+"\",dz:\"info?lx=1&id="+tXzjg.getId()+"\",open: false},";
                //放入二级菜单
                tXzjgListSon = new ArrayList<>();
                tXzjgListSon = tXzjgDao.getXzjgXjNoRoot(tXzjg.getId());
                for (int k=0;k<tXzjgListSon.size();k++){
                    tXzjgSon = new TXzjg();
                    tXzjgSon = tXzjgListSon.get(k);
                    treejson += "{id:\""+tXzjgSon.getId()+"\",pId:\""+tXzjgSon.getPid()+"\", name: \""+tXzjgSon.getName()+"\",dz:\"info?lx=1&id="+tXzjgSon.getId()+"\",open: false},";
                }
            }
            rtnMap.put("treejson", JSON.parse("["+treejson+"]"));
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    public ResultInfo info(String loginId, Map<String, Object> map){
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="",id="",pid="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("pid")) {
                pid = jsonObject.getString("pid");
            }
            Map<String,Object> rtnMap = new HashMap<>();

            if ("1".equals(lx)){
                TXzjg tXzjg = new TXzjg();
                tXzjg = tXzjgDao.getXzjgById(id);
                rtnMap.put("tXzjg",tXzjg);
            }
            if (!StringUtils.isEmpty(pid)){
                TXzjg pXzjg = new TXzjg();
                pXzjg = tXzjgDao.getXzjgById(pid);
                if (null==pXzjg){
                    return new ResultInfo(500,"上级机构信息丢失！",null);
                }
                rtnMap.put("pXzjg",pXzjg);
            }

            //可以迁移的机构
            List<TXzjg> tXzjgList = new ArrayList<>();
            tXzjgList = tXzjgDao.selectList(new QueryWrapper<TXzjg>().eq("jb","1").orderByAsc("xh"));
            rtnMap.put("tXzjgList",tXzjgList);

            ResultInfo resultInfo = new ResultInfo(0, "查询成功！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo save(String loginId, Map<String, Object> map) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            JSONObject jsonObject = getData(map,loginId);
            String lx="",id="",nonce_ms="",nonce_zhi="",jgmc="",jgh="",xsxh="",pid="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("jgmc")) {
                jgmc = jsonObject.getString("jgmc");
            }
            if (jsonObject.containsKey("jgh")) {
                jgh = jsonObject.getString("jgh");
            }
            if (jsonObject.containsKey("xsxh")) {
                xsxh = jsonObject.getString("xsxh");
            }
            if (jsonObject.containsKey("pid")) {
                pid = jsonObject.getString("pid");
            }

            if (StringUtils.isEmpty(jgmc)){
                ResultInfo resultInfo = new ResultInfo(500, "机构名称不能为空！",null);
                return backRtn(resultInfo,loginId);
            }

            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceService.checkNonce(nonce_ms,loginId,nonce_zhi,"xzjg.addDo");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            TCzy tCzy = gettCzyDao().getCzyById(loginId);
            TXzjg tXzjg = new TXzjg();
            TXzjg pXzjg = new TXzjg();
            if ("1".equals(lx)){
                tXzjg = tXzjgDao.selectById(id);
            }
            tXzjg.setName(jgmc);
            tXzjg.setZjm(ToolBean.getPy(jgmc));

            if (!StringUtils.isEmpty(pid)){
                pXzjg = tXzjgDao.selectById(pid);
            }

            //如果有上级，级别为上级的级别+1
            if (!StringUtils.isEmpty(pid)){
                tXzjg.setPid(Long.valueOf(pid));
                tXzjg.setJb(pXzjg.getJb()+1);
            }else{
                //如果是修改，不改变级别，如果是新增，则都是第一级
                if ("0".equals(lx)) tXzjg.setJb(1L);
            }

            if (!StringUtils.isEmpty(xsxh)) tXzjg.setXh(Long.valueOf(xsxh));
            ResultInfo resultInfo;
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("name",jgmc);
            if ("1".equals(lx)){
                tXzjgDao.updateById(tXzjg);
                resultInfo = new ResultInfo(2, "修改成功！",rtnMap);
            }else{
                tXzjgDao.insert(tXzjg);
                resultInfo = new ResultInfo(1, "新增成功！",rtnMap);
            }
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo qy(String loginId, Map<String, Object> map) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            JSONObject jsonObject = getData(map,loginId);
            String id="",new_pid="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("new_pid")) {
                new_pid = jsonObject.getString("new_pid");
            }

            if (StringUtils.isEmpty(new_pid)){
                ResultInfo resultInfo = new ResultInfo(500, "新上级名称不能为空！",null);
                return backRtn(resultInfo,loginId);
            }

            if (StringUtils.isEmpty(id)){
                ResultInfo resultInfo = new ResultInfo(500, "关键参数丢失！",null);
                return backRtn(resultInfo,loginId);
            }

            TCzy tCzy = gettCzyDao().getCzyById(loginId);
            TXzjg tXzjg;
            tXzjg = tXzjgDao.selectById(id);
            tXzjg.setPid(Long.parseLong(new_pid));
            tXzjgDao.updateById(tXzjg);
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("name",tXzjg.getName());
            return backRtn(new ResultInfo(1, "迁移成功！",rtnMap),loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo del(String loginId, Map<String, Object> map) {
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            JSONObject jsonObject = getData(map,loginId);
            String menu_id="";
            if (jsonObject.containsKey("id")) {
                menu_id = jsonObject.getString("id");
            }
            if (StringUtils.isEmpty(menu_id)){
                ResultInfo resultInfo = new ResultInfo(500, "没有需要删除的信息！",null);
                return backRtn(resultInfo,loginId);
            }
            long sl = tXzjgDao.selectCount(new QueryWrapper<TXzjg>().eq("pid",menu_id.trim()));
            if (sl>=1){
                ResultInfo resultInfo = new ResultInfo(500, "请先删除下级数据！",null);
                return backRtn(resultInfo,loginId);
            }else {
                tXzjgDao.deleteById(menu_id);
                ResultInfo resultInfo = new ResultInfo(3, "删除成功！",null);
                return backRtn(resultInfo,loginId);
            }
        }
    }
}
